<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import Commodityedit from "./../commodityedit/index";
export default {
  mixins: [
    Vue.prototype.$vFN.Mixins.Base,
    Vue.prototype.$vFN.Mixins.Edit,
    Vue.prototype.$vFN.Mixins.List,
  ],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
  },
  data() {
    return {
      appWindow: window,
      loading: true,
      // 页面接口地址
      apiUrl: {
        create: "api/time-discount/createDiscountProduct",
        list: "api/time-discount/pageDiscountProduct",
        delete: "api/time-discount/deleteDiscountProduct",
        updata: "api/time-discount/updateDiscountProduct",
      },
      activityStatus: this.activityStatus,

      // 请求携带参数
      mixinList_tableSearchConst: {
        timeDiscountId: "",
      },
      // tableData: [],
      addGoodsArr: [], //选择的商品数组
      goodsIds: [],

      // 表格数据删除展示结构
      tableDeleteConfig: {
        width: "400px",
        title: "确定删除?",
        columns: [
          {
            label: "商品名称",
            prop: "productName",
            type: "",
          },
        ],
      },
    };
  },
  methods: {
    // 初始化
    async init() {
      // 显示窗口
      // this.getList();
      this.mixinList_getTable();
      this.mixinEdit_editVisible = true;
    },

    SelectDataGoods() {
      this.$vFN.Directives.SelectDataGoods({
        appWindow: this.appWindow,
        selectIds: this.goodsIds,
        apiConfig: this.$vFN.Tools.getAppGlobalData(
          "componentsApiConfig.SelectDataGoods",
          this.appWindow
        ),
        count: 10,
        onSuccess: (res) => {
          console.error(res);
          this.goodsIds = res;
          this.addGoodsDiscount();
        },
      });
    },

    // 创建
    async addGoodsDiscount() {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.create,
          data: {
            productIdLst: this.goodsIds, // 商品集合
            timeDiscountId: this.id, //从 home 传来的(限时折扣的id)
          },
        },
        window
      );

      if (res._ok) {
        this.mixinList_getTable(); // 创建成功后回显
      }
    },

    // 删除(移除)单个
    async delGoodsDiscount(id_) {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.delete,
          data: {
            ids: [id_],
          },
        },
        window
      );
      if (res._ok) {
        this.mixinList_getTable();
      }
    },

    updateGoodsDiscount(params_) {
      Commodityedit.init({
        type: params_.type,
        ids: params_.ids,
        onSuccess: (res) => {},
      });
    },

    // 提交成功
    onSubmitSuccess() {
      this.onSuccess();
    },
    // 成功回调
    onSuccess() {},
    // 取消回调
    onCancel() {},
    // 失败回调
    onError() {},
  },
  mounted() {
    this.mixinList_tableSearchConst.timeDiscountId = this.id;
    this.init();
    setTimeout(() => {
      this.loading = false;
    }, 1000);
  },
  activated() {},
};
</script>